package com.yanqu.road.server.cmd.activity.dafuweng.cross;

import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.pb.monopoly.MonopolyProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.dafuweng.DaFuWengMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.ArrayList;
import java.util.Map;

@Cmd(code = GameProtocol.S_DA_FU_WENG_CROSS_UNION_SYNC_DATA,desc = "")
public class DfwSyncUnionDataCmd implements Command {
    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        MonopolyProto.SyncUnionDataMsg reqMsg = MonopolyProto.SyncUnionDataMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        String unionId = reqMsg.getUnionId();
        MonopolyProto.UnionDataMsg unionMsg = reqMsg.getUnionMsg();
        Map<Long, CrossUnionMember> memberMap = CrossUnionMgr.getUnionMemberMap(unionId);
        DaFuWengMgr.updateUnionMsg(activityId,unionId, unionMsg);
        for (CrossUnionMember member : new ArrayList<>(memberMap.values())) {
            if (member.getServerId() == GameServer.getInstance().getServerId()) {
                GamePlayer player = GamePlayerMgr.getOnlinePlayer(member.getUserId());
                if (player != null) {
                    player.sendPacket(ClientProtocol.U_DA_FU_WENG_UNION_DATA, unionMsg.toBuilder());
                }
            }
        }
    }
}
